package app.netant.bt;

import java.lang.reflect.Constructor;

import app.esb.common.BussRef;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public abstract class MargnetRoot {
	
	public void analyse(int minIdx,int maxIdx){
		
		System.out.println("整体处理开始........");
		
		//断点续传
		//resume();
		
		for(int i=minIdx;i<=maxIdx;i++){
			
			String url = this.getUrl(i);
				
			System.out.println("第"+i+"页数据开始处理！");
			
			try {		
				MargnetPage page = this.buildPage(url);
				page.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_MARGNET_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+MathUtil.random(3)+"','"+url+"','1')");					
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public void analyse(int maxIdx){
		
		System.out.println("整体处理开始........");
		
		//断点续传
		//resume();
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = this.getUrl(i);
				
			System.out.println("第"+i+"页数据开始处理！");
			
			try {		
				MargnetPage page = this.buildPage(url);
				page.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_MARGNET_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						                            + "values ('"+System.currentTimeMillis()+MathUtil.random(3)+"','"+url+"','1')");					
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public void error(){
		
		System.out.println("清理错误数据........");
		
		DBFactory.getThreadDB().executeUpdate("delete from ${schema}.NET_MARGNET_SRC "
				                   + "where SRC_URL in "
				                   + "(select EXCEP_URL from ${schema}.NET_MARGNET_EXCEP)");
		
		DBFactory.getThreadDB().executeUpdate("delete from ${schema}.NET_MARGNET_SRC "
							                + "where MARGNET_ID not in "
							                + "(select MARGNET_ID from ${schema}.NET_MARGNET_TOL)");
		
		DBFactory.getThreadDB().executeUpdate("delete from ${schema}.NET_MARGNET_TOL "
									        + "where MARGNET_ID not in "
									        + "(select MARGNET_ID from ${schema}.NET_MARGNET_SRC)");
		
		System.out.println("异常处理开始........");
		
		DataTable table = DBFactory.getThreadDB().executeQueryTable("select * from ${schema}.NET_MARGNET_EXCEP");
		while(table.next()){
			try {
				
				String expID = table.getString("EXCEP_ID");
				String expUrl = table.getString("EXCEP_URL");
				String expLevel = table.getString("EXCEP_LEVEL");
				
				System.out.println("异常地址："+expUrl+"开始处理.......");
				
				if("1".equals(expLevel)){
					
					MargnetPage page = this.buildPage(expUrl);
					page.analyse();
					
				}else if("2".equals(expLevel)){
					
					Enviroment.getEnv().getLogBean().beginLog();

					DataTable srcTable = DBFactory.getThreadDB().executeQueryTable("select * from ${schema}.NET_MARGNET_SRC where SRC_URL='"+expUrl+"'");
					if(srcTable.next()){
						return;
					}
					
					MargnetEntity entity = new MargnetEntity();
					entity.setSrcURL(expUrl);
								
			        MargnetDetail detail = this.buildDetail(expUrl);
					
					detail.setEntity(entity);
					detail.analyse();
					
					entity.insert();
					
					Enviroment.getEnv().getLogBean().endLog();
					
				}
					
				DBFactory.getThreadDB().executeUpdate("delete from ${schema}.NET_MARGNET_EXCEP where EXCEP_URL='"+expUrl+"'");
				
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
		
		System.out.println("异常处理结束........");
		
	}
	
	public abstract String getUrl(int idx);
	public abstract MargnetPage buildPage(String url);
	public abstract MargnetDetail buildDetail(String url);
	
}
